import pylab as plt
import numpy as np
from params import *
import os
import sys
import math

def summary(data):
    print 'Data in [%f,%f]'%(min(data),max(data))
    print '%d values'%len(data)
    print 'Average : ', sum(data)/len(data)
    zero_values = sum(1 for x in data if not x > 0)
    print 'zero values ', zero_values
    data = filter(lambda x: x > 0, data)
    print 'Average nonzero : ', sum(data)/len(data)

def ecdf(data):
    data2 = [d for d in data if d > 0]
    N = 200
    l1 = 0
    l2 = math.log10(max(data2)) 
    l2 = math.ceil(l2)
    bins = 10**np.linspace(l1,l2, N)
    bins = 10**np.linspace(-4, 0, N)

    pdf1,bins1,x = plt.hist(data2,
            bins=bins,cumulative=False,normed=False)
    plt.close()

    c = [pdf1[0]]
    for j in range(1,len(pdf1)):
        t = c[j-1]
        c.append(t+pdf1[j])

    total = c[-1]
    c1 = map(lambda x: 1.0*x/total, c)
    return bins1[:-1],c1

datasets = ['bkite','fsquare','livejournal', 'twitter']

plots = []
for dataset in datasets:
    tracefile = os.path.join(WORKDIR, 'gsn', 'results',dataset,
      #'%s_node_geoclustering.txt'%dataset)
      '%s_local_clustering.txt'%dataset)
    geoclustering = []
    for line in open(tracefile):
        node, gc = line.strip().split(' ')
        gc = float(gc)
        if gc < 0:
          gc = 0.0
        if gc > 1:
          gc = 1.0
        geoclustering.append(gc)

    print dataset
    summary(geoclustering)
    print ''

    x,c = ecdf(geoclustering)
    plots.append((x,c))
    print x, c

plt.figure()
plt.clf()
plt.axes(FIG_AXES2)
for (x,c), m in zip(plots, markers):
  plt.semilogx(x,c,'k%s'%m, mfc='None')
lbls = ['Brightkite', 'Foursquare', 'LiveJournal', 'Twitter']
plt.legend(lbls,loc='upper left',numpoints=1, ncol=1)
plt.ylabel('CDF')
plt.xlabel('Geographic clustering')
plt.grid(True)
plt.savefig('four_geoclustering.pdf')
plt.close()

